package javaweb.dao.impl;

import javaweb.dao.BookDao;
import javaweb.pojo.Book;

import java.util.List;

/**
 * @author shkstart
 * @creat -07-26 20:56
 */
public class BookDaoImpl extends BaseDao implements BookDao {
    @Override
    public int  addBook(Book book) {
String sql="INSERT INTO t_book(`name`,`author`,`price`,`sales`,`stock`,`img_path`) values(?,?,?,?,?,?)";
return upDate(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImg_path());
    }
    @Override
    public int  upDateBook(Book book) {
String sql="upDate t_book set `name`=?," +
        "`author`=?,`price`=?," +
        "`sales`=?,`stock`=?,`img_path`=? where id=?";
        int i = upDate(sql, book.getName(),
                book.getAuthor(), book.getPrice(),
                book.getSales(), book.getStock(), book.getImg_path(),
                book.getId());
        System.out.println(i);
        return i;
    }
    @Override
    public Book queryBookId(Integer id) {
     String sql="select `id`,`name`,`author`,`price`,`sales`,`stock`,`img_path` imgPath from t_book where id=?";
        return quryForOne(Book.class,sql,id);
    }
    @Override
    public int  deleteBookId(Integer id) {
        String sql="delete from t_book where id=?";
        return upDate(sql,id);

    }

    @Override
    public List<Book> queryBooks() {
       String sql ="select `id`, `name`,`author`,`price`,`sales`,`stock`,`img_path` imgPath from t_book";
        return queryForList(Book.class,sql);
    }

    @Override
    public Integer queryForTocalCount() {
String sql="select count(*) from t_book";
Number count= (Number) queryForSingValue(sql);
        return count.intValue();
    }

    @Override
    public List<Book> queryForPageItems(Integer begin, Integer pageSize) {
        String sql="select *from t_book limit ?,?";
        return queryForList(Book.class,sql,begin,pageSize);
    }

    @Override
    public Integer queryForTocalCountByPrice(int min, int max) {
       String sql="select count(*) from t_book where price between ? and ?  ";
        Number count= (Number) queryForSingValue(sql,min,max);

        return   count.intValue();
    }

    @Override
    public List<Book> queryForPageItemsByPrice(int begin, int pageSize,int min,int max) {
        String sql="select *from t_book where price between ? and ? order by price limit ?,?";
        return queryForList(Book.class,sql,min,max,begin,pageSize);


    }


}
